package top.huhuiyu.myforum.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import top.huhuiyu.myforum.entity.TbSubject;
import top.huhuiyu.myforum.model.TbSubjectModel;
import top.huhuiyu.myforum.dao.TbSubjectDAO;
import top.huhuiyu.myforum.util.JsonMessage;

/**
 * TbSubject的控制器类
 */
@RestController
@RequestMapping("/tbSubject")
public class TbSubjectController {
  
  @Autowired
  private TbSubjectDAO tbSubjectDAO;
  
  @RequestMapping("/save")
  public JsonMessage save(TbSubjectModel tbSubjectModel) throws Exception {
    // 测试模板 /tbSubject/save {"tbSubject.isEnable":"1","tbSubject.sname":"2","tbSubject.description":"3"}
    tbSubjectDAO.save(tbSubjectModel.getTbSubject());
    return JsonMessage.getSuccessMessage("数据保存成功");
  }
  
  @RequestMapping("/update")
  public JsonMessage update(TbSubjectModel tbSubjectModel) throws Exception {
    // 测试模板 /tbSubject/update {"tbSubject.sid":"1","tbSubject.isEnable":"2","tbSubject.sname":"3","tbSubject.description":"4"}
    tbSubjectDAO.update(tbSubjectModel.getTbSubject());
    return JsonMessage.getSuccessMessage("数据修改成功");
  }
  
  @RequestMapping("/delete")
  public JsonMessage delete(TbSubjectModel tbSubjectModel) throws Exception {
    // 测试模板 /tbSubject/delete {"tbSubject.sid":"1"}
    tbSubjectDAO.delete(tbSubjectModel.getTbSubject());
    return JsonMessage.getSuccessMessage("数据删除成功");
  }
  
  @RequestMapping("/queryByKey")
  public JsonMessage queryByKey(TbSubjectModel tbSubjectModel) throws Exception {
    // 测试模板 /tbSubject/queryByKey {"tbSubject.sid":"1"}
    TbSubject tbSubject = tbSubjectDAO.queryByKey(tbSubjectModel.getTbSubject());
    JsonMessage message = JsonMessage.getSuccessMessage("数据查询完成");
    message.putData("tbSubject",tbSubject);
    return message;
  }
  
  @RequestMapping("/queryAll")
  public JsonMessage queryAll(TbSubjectModel tbSubjectModel) throws Exception {
    // 测试模板 /tbSubject/queryAll {}
    PageHelper.startPage(tbSubjectModel.getPage().getPageNumber(), tbSubjectModel.getPage().getPageSize());
    Page<TbSubject> list = (Page<TbSubject>) tbSubjectDAO.queryAll();
    tbSubjectModel.getPage().setPageInfo(list);
    JsonMessage message = JsonMessage.getSuccessMessage("查询成功！");
    message.putData("list", list);
    message.putData("page", tbSubjectModel.getPage());
    return message;
  }
  
}
